
package com.gegao.practise.lanuchcount;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gegao.practise.R;

public class OrangeGestureHelp {
    private static OrangeGestureHelp mInstance;
    RelativeLayout.LayoutParams mParams;
    protected Activity mActivity;
    ViewGroup mParent;
    private View mBaseUi;
    private Handler mHandler;
    protected View mTip;
    TextView mSelectArticle;

    public static final String GESTURE_SHARED_FILE = "gesture_help";
    public static final String APP_LAUNCH_COUNT = "app_launch_count";
    public static final String ALREADY_USE_JIANBAO_GESTURE = "already_use_jianbao_gesture";
    private final static int JIANBAO_SHOW_LIMIT = 1;

    public static final String SHOW_JIANBAO_EVERY_LAUNCH = "show_jianbao_every_launch";

    private OrangeGestureHelp(Activity activity, ViewGroup parent) {
        mActivity = activity;
        mParent = parent;
        mParams = new RelativeLayout.LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        mHandler = new Handler();
    }

    public static OrangeGestureHelp getInstantce(Activity activity, ViewGroup parent) {
        if (null == mInstance) {
            mInstance = new OrangeGestureHelp(activity, parent);
        }
        return mInstance;
    }

    public void onPageFinished(boolean homepage) {
        if (homepage) {
             showTryTip();
        } else {
            SharedPreferences prefs =
                    mActivity.getSharedPreferences(GESTURE_SHARED_FILE, 0);
            boolean flag = prefs.getBoolean(SHOW_JIANBAO_EVERY_LAUNCH, false);
            int launch_count = prefs.getInt(APP_LAUNCH_COUNT, 0);
            if (launch_count > JIANBAO_SHOW_LIMIT && flag && !getPrefsProperty(ALREADY_USE_JIANBAO_GESTURE)) {
                showJianBaoHelpTip();
            }
        }
    }
    boolean isTryTipShowing = false;
    
    public void showJianBaoHelpTip() {
        showHelpTip(R.drawable.rectangle_tip,R.string.gesture_jianbao_tip);
        setPrefsProperty(SHOW_JIANBAO_EVERY_LAUNCH,
                false);
    }

    private void showHelpTip(int picId,int textId) {
        mTip = LayoutInflater.from(mActivity).inflate(R.layout.tip_gesture_share,
                (ViewGroup) mActivity.findViewById(R.id.tip_toast));
        mSelectArticle = (TextView) mTip.findViewById(R.id.gesture_tip);

        mSelectArticle.setText(textId);
        mSelectArticle
                .setCompoundDrawablesWithIntrinsicBounds(picId, 0, 0, 0);
        Toast toast = new Toast(mActivity);
        toast.setView(mTip);
        toast.show();
        
    }

    public void setPrefsProperty(String key, boolean flag) {
        SharedPreferences prefs =
                mActivity.getSharedPreferences(OrangeGestureHelp.GESTURE_SHARED_FILE, 0);
        prefs.edit().putBoolean(key,
                flag).commit();
    }
    
    public boolean getPrefsProperty(String key) {
        SharedPreferences prefs =
                mActivity.getSharedPreferences(OrangeGestureHelp.GESTURE_SHARED_FILE, 0);
        return prefs.getBoolean(key,false);
               
    }
    
//    public void showTryTipMulti(final FrameLayout parent) {
//        final View tip = LayoutInflater.from(mActivity).inflate(R.layout.tip_gesture_try, null);
//
//        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
//                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
//                android.view.ViewGroup.LayoutParams.MATCH_PARENT);
//        parent.addView(tip, params);
//        tip.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                parent.removeView(tip);
//            }
//        });
//        Toast.makeText(mActivity, "showTryTip", Toast.LENGTH_LONG).show();
//    }
    
    public void showTryTipMulti(final FrameLayout parent) {
        mTip = LayoutInflater.from(mActivity).inflate(R.layout.tip_gesture_try, null);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT);
        parent.addView(mTip, params);
        mTip.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                parent.removeView(mTip);
            }
        });
    }
    
    private void showTryTip() {
        mTip = LayoutInflater.from(mActivity).inflate(R.layout.tip_gesture_try, null);
        if (mTip.isShown()) {
            return;
        }
        android.util.Log.d("jb", "showTryTip()");

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT);
        mParent.addView(mTip, params);
        isTryTipShowing = true;
        mTip.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mParent.removeView(mTip);
                mTip = null;
            }
        });
    }
    
}
